package com.zhidisoft.crm.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.zhidisoft.crm.entity.SysRole;
import com.zhidisoft.crm.vo.SysRoleVo;

/**
 * 角色的增删改查
 * @author Administrator
 *
 */
public interface ISysRoleMapper {
	/**
	 * 添加角色
	 * @param role
	 */
	@Insert("insert into sys_role(id,role_name,role_info)"
			+ " values(#{id},#{role_name},#{role_info})")
	void save(SysRole role);
	
	/**
	 * 根据用户名查询用户角色
	 * @param username
	 */
	@Select("select r.* from sys_role r inner join sys_user_role sur on sur.role_id=r.id "
			+ "inner join sys_user su on su.id=sur.user_id where su.username=#{username}")
	 SysRole findByuser(String username);
	
	@Select("select * from sys_role where id=#{id}")
	SysRole selectById(String id);
	
	/**
	 * 查询所有
	 * @param id
	 * @return
	 */
	@Select("select * from sys_role")
	List<SysRole> selectAll();
	/**
	 * 分页查询所有角色
	 * @param startIndex
	 * @param pageSize
	 * @return
	 */
	@Select("SELECT * FROM("
			+ "SELECT A.*,ROWNUM RN "
			+ "FROM "
			+ " (SELECT * FROM SYS_ROLE ) A"
			+ " WHERE ROWNUM<=#{endIndex})  TL "
			+ "WHERE RN > #{startIndex}")
	List<SysRole> pageList(@Param("startIndex") Integer startIndex, @Param("endIndex") Integer endIndex);
	/**
	 * 获取角色总条数
	 * @return
	 */
	@Select("SELECT count(id) FROM sys_role")
	Long totalCount();
	/**
	 * 根据id删除角色
	 * @param id
	 */
	@Delete("delete from sys_role where id=#{id}")
	void deleteById(String id);
	
	@Update("update sys_role set role_name=#{role_name},role_info=#{role_info}where id=#{id}")
	void update(SysRole sysRole);
}
